package palette;

import javax.swing.*;

import java.awt.*;

import actionHandlers.ShapeDraw;
import cuShapes.*;
import cuShapes.Box;

import java.util.List;


/**************************************************
*
**************************************************/
public class Canvas extends JPanel{

	private static final long serialVersionUID = 1L;
	private Paint bgColor = Color.white;
	private Paint brushColor = Color.black;
	private ShapeDraw shapeDrawer; 
	private Boolean hasGrid = false;
	//private ArrayList<PQShape> shapeList;  removed. Now in shapeDrawer.  -jd 3.30.10
	
	/**********************************************
	 *   CONSTRUCTOR
	**********************************************/
	public Canvas(){
		//shapeList = new ArrayList<PQShape>(); //removed -jd 3.30.10
		shapeDrawer = new ShapeDraw();//shapeList);
		this.addMouseListener(shapeDrawer);
		this.addMouseMotionListener(shapeDrawer);
		this.setPreferredSize(new Dimension(500,400));
		this.setVisible(true);		
	}

	/**********************************************
	 *   paint() Draws the lines onto the screen
	**********************************************/
	public void paint(Graphics g){
		
		Graphics2D g2 = (Graphics2D)g;
		
		//fill background
	    g2.setPaint(bgColor);
	    g2.fillRect(0, 0, this.getWidth(), this.getHeight());

	    //draw grid
	    if (hasGrid) 
	    {
	    	drawGrid(g);
	    }
	    
	    //draw all shapes
		shapeDrawer.drawAll(g);
	    g2.setPaint(brushColor);
	}
	
	/******************************************
	 *  Clears the contenst of the shape list
	 *  
	 *  changed from local array list .clear(), to
	 *  shapeDrawer.clear() (a new method that clears the
	 *  arrayList in the shapeDrawer.
	 *    -JD  3.30.10
	 */
	public void clear() {
		shapeDrawer.clear();
		Arrow.resetNames();
		Box.resetNames();
		Ellipse.resetNames();
		repaint();
	}
	/******************************************
	 *   Gets the shape List
	 */

	public List<PQShape> getShapeList(){  //added jd  3.30.10
		return shapeDrawer.getShapeList();
	}
	public List<PQShape> getArrowList(){   //added -jd 3.30.10
		return shapeDrawer.getArrowList();
	}
	
	public PQShape getCurrentShape() {
		return shapeDrawer.getCurrentShape();
	}
	public void setCurrentShape(PQShape s) {
		shapeDrawer.setCurrentShape(s);
	}
	
	public void drawGrid(Graphics g) {
		
		int wide = getWidth();
		int tall = getHeight();

		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.lightGray);

		g2.setStroke(new BasicStroke(1));
		// the vertical lines
		for (int i = 10; i < wide; i+=10) {
			g2.drawLine(i, 0, i, tall);
		}
		// the horizontal lines
		for (int i = 10; i < tall; i+=10) {
			g2.drawLine(0, i, wide, i);
		}
	}
	
	public void setGrid(Boolean b) {
		hasGrid = b;
	}
}
